import dash
from dash import html, dcc
from dash.dependencies import Input, Output
from flask_login import current_user
import feffery_utils_components as fuc

from server import app
from config import RouterConfig

# 载入子页面
import views

app.layout = html.Div(
    [
        # 注入url监听
        dcc.Location(id='url'),

        fuc.FefferyExecuteJs(id='execute-js'),

        # 背景粒子动画挂载点
        html.Div(
            id='particles-mount',
            style={
                'position': 'fixed',
                'top': 0,
                'left': 0,
                'right': 0,
                'bottom': 0,
                'zIndex': -1
            }
        ),

        # 注入页面内容挂载点
        html.Div(id='app-mount'),

        # 路由重定向
        html.Div(id='router-redirect-container'),

        # 注入消息提示容器
        html.Div(
            [
                # "用户管理-新增用户"消息提示
                html.Div(id='index-user-manage-add-user-message-container'),
            ]
        )
    ]
)


@app.callback(
    Output('execute-js', 'jsString'),
    Input('url', 'pathname')
)
def render_js(pathname):
    if pathname != '/login':
        return dash.no_update

    return '''
// 第一个参数对应粒子效果挂载的容器id
particlesJS('particles-mount',
    {
        "particles": {
            "number": {
                "value": 80,
                "density": {
                    "enable": true,
                    "value_area": 800
                }
            },
            "color": {
                "value": "#c7d2dd"
            },
            "shape": {
                "type": "circle",
                "stroke": {
                    "width": 0,
                    "color": "#000000"
                },
                "polygon": {
                    "nb_sides": 5
                }
            },
            "opacity": {
                "value": 0.5,
                "random": false,
                "anim": {
                    "enable": false,
                    "speed": 1,
                    "opacity_min": 0.1,
                    "sync": false
                }
            },
            "size": {
                "value": 5,
                "random": true,
                "anim": {
                    "enable": false,
                    "speed": 40,
                    "size_min": 0.1,
                    "sync": false
                }
            },
            "line_linked": {
                "enable": true,
                "distance": 150,
                "color": "#c7d2dd",
                "opacity": 0.4,
                "width": 1
            },
            "move": {
                "enable": true,
                "speed": 2,
                "direction": "none",
                "random": false,
                "straight": false,
                "out_mode": "out",
                "attract": {
                    "enable": false,
                    "rotateX": 600,
                    "rotateY": 1200
                }
            }
        },
        "retina_detect": true,
        "config_demo": {
            "hide_card": false,
            "background_color": "#b61924",
            "background_image": "",
            "background_position": "50% 50%",
            "background_repeat": "no-repeat",
            "background_size": "cover"
        }
    }
);   
'''


@app.callback(
    [Output('app-mount', 'children'),
     Output('router-redirect-container', 'children')],
    Input('url', 'pathname')
)
def router(pathname):

    # 检验pathname合法性
    if pathname not in RouterConfig.VALID_PATHNAME:
        # 渲染404状态页
        return [
            views._404.render_content(),
            None
        ]

    # 检查当前会话是否已经登录
    # 若已登录
    if current_user.is_authenticated:
        # 根据pathname控制渲染行为
        if pathname == '/login':
            # 重定向到主页面
            return [
                dash.no_update,
                dcc.Location(
                    pathname='/',
                    id='router-redirect'
                )
            ]

        # 否则正常渲染主页面
        return [
            views.index.render_content(
                current_user.id,
                current_user.gender,
                current_user.role,
                current_user.register_time
            ),
            None
        ]

    # 若未登录
    # 根据pathname控制渲染行为
    if pathname == '/login':
        return [
            views.login.render_content(),
            None
        ]

    # 否则重定向到登录页
    return [
        dash.no_update,
        dcc.Location(
            pathname='/login',
            id='router-redirect'
        )
    ]


if __name__ == '__main__':
    app.run(debug=True)
